package UserInterface;
import Database.DatabaseOperator;
import javafx.concurrent.Task;
import javafx.concurrent.Worker;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import main.SearchManager;
import search.FileHandler;
import search.impl.Spider;
import vo.Program;

import java.lang.reflect.Field;
import java.util.ArrayList;


public class SpiderWindow {

    public CheckBox taughtCheckBox;
    public CheckBox searchCheckBox;
    public Label timeLabel;
    public Button okButton;
    public AnchorPane page1;
    public AnchorPane page2;
    public AnchorPane mainPage;
    public Button yes;
    public Button no;
    public Button saveInLocalTxt;
    public AnchorPane page3;
    public Label label;
    private DatabaseOperator operator;
    private int type = 0;
    private ArrayList<Program> programList = new ArrayList<Program>();
    public void init(DatabaseOperator operator) {
        this.operator = operator;
        for (Field field : SpiderWindow.class.getDeclaredFields()) {
            if(field.getName().contains("CheckBox"))
            {
                try {
                    CheckBox checkBox = (CheckBox) field.get(this);
                    checkBox.selectedProperty().addListener((observable, oldValue, newValue) ->
                    {
                        boolean isTaught = taughtCheckBox.isSelected();
                        boolean isResearch = searchCheckBox.isSelected();
                        double time = 0;
                        if (isTaught && isResearch)
                        {
                            time = 5;
                            this.type = 3;
                        }
                        else if(!isTaught && isResearch)
                        {
                            time = 5;
                            this.type = 2;
                        }
                        else if (isTaught && !isResearch)
                        {
                            time = 10;
                            this.type = 1;

                        }
                        else
                        {
                            this.type = 0;
                            time = 0;
                        }
                        if(time!= 0) {
                            String format = String.format("这个过程大概需要%f2分钟，可以接受吗？", time);
                            this.timeLabel.setText(format);
                        }
                        else this.timeLabel.setText("");
                    });
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }

            }
        }
    }

    public void okButton_onClick(MouseEvent mouseEvent) {
        try {

            ArrayList<Program> programs = new ArrayList<>();


            programs = Spider.spider(type);

            this.mainPage.getChildren().remove(page1);
            this.page2.setVisible(true);
            this.page2.setVisible(false);
            page3.setVisible(true);
            this.programList = programs;
        }catch (IndexOutOfBoundsException indexE)
        {
            this.timeLabel.setText("先选个内容吧");
        }
        close(mouseEvent);
    }

    public void yes_onClick(MouseEvent mouseEvent)
    {
        System.out.println("programList == null: " +(programList == null));
        System.out.println("operator == null: " + (operator == null));
        for (Program program : programList)
        {
            if (program != null)
            {
                this.operator.insert(program);
            }
        }
    }
    public void no_onClick(MouseEvent mouseEvent)
    {
        close(mouseEvent);
    }

    public void close(MouseEvent mouseEvent)
    {
        ((Stage)((Node)mouseEvent.getSource()).getScene().getWindow()).close();
    }

    public void saveInLocalTxt_onClick(MouseEvent mouseEvent)
    {
        FileHandler fileHandler = SearchManager.getFileHandler();
        fileHandler.program2File(programList);
        close(mouseEvent);
    }

}
